
package com.monlyu.bo;

import java.io.File;

import org.apache.commons.io.FileUtils;
import org.springframework.stereotype.Repository;

import com.monlyu.util.ArrayUtil;
import com.monlyu.util.DateUtil;
import com.monlyu.util.FileUtil;
import com.monlyu.util.ImageUtils;
import com.monlyu.util.LogUtil;
import com.monlyu.util.StringUtil;
import com.monlyu.util.WebOptUtil;
import com.monlyu.vo.Attachment;

/********************************************************************************
 * <b>功能描述☞</b><br>
 * <b>开发公司☞</b>重庆唯尼科技有限公司<br>
 * <b>版权☞</b>本文件归重庆唯尼科技有限公司所有<br>
 * <b>项目名☞</b>monlyu<br>
 * <b>文件路径☞</b>com.monlyu.bo.UploadFileBo.java<br>
 * <b>开发日期☞</b>2010-1-29 <br>
 * 
 * @author monlyu
 * @since JDK 5
 * @version 1.0.8
 ********************************************************************************/
@Repository(value = "attachmentBo")
public class AttachmentBo extends BaseBo {

    /**
     * 保存文件到制定的地方
     * 
     * @param file
     *            文件
     * @param name
     *            文件名
     * @param type
     *            类型
     * @author monlyu
     * @since 2010-1-29
     */
    public String saveFile(File file, String name, String type, String desc){
        String datePath = DateUtil.format(DateUtil.now(), "/yyyy/MM/dd");
        File descFile = new File(WebOptUtil.APP_ARGS.getUploadDir() + datePath);
        if (!descFile.exists()) {
            descFile.mkdirs();
        }
        // File toDir = new File(WebOptUtil.APP_ARGS.getUploadDir());
        Attachment attachment = new Attachment();
        attachment.setDate(DateUtil.now());
        attachment.setLocalName(FileUtil.randFName(name));
        attachment.setName(name);
        attachment.setPath(datePath);
        attachment.setType(type);
        attachment.setUser(curUser());
        attachment.setSize(file.length());
        attachment.setMark(desc);
        dao.insertOrUpdate(attachment);
        try {
            File targetFile = new File(descFile, attachment.getLocalName());
            FileUtils.copyFile(file, targetFile);
            String[] vaildExt = { ".jpg", ".png", ".jpeg", ".gif" };
            if (ArrayUtil.indexOf(vaildExt, name.substring(name.lastIndexOf("."))) > -1) {
                ImageUtils.scaleSysnc(file.getAbsolutePath(), FileUtil.appendFileName(targetFile, "_128"), 128);
            }
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        String uploaded = datePath + "/" + FileUtil.appendFileName(attachment.getLocalName(), "_128");
        return attachment.getId()
            + "_#_"
            + desc
            + "_#_"
            + StringUtil.path2Uri(uploaded);
    }

    /**
     * 获取附件对象信息
     * 
     * @author monlyu
     * @since 2010-1-30
     */
    public Attachment findAttch(Long id){
        return dao.get(Attachment.class, id);
    }

    /**
     * 创建对应的
     */
    public void createDir(){
        File file = new File("/Users/monlyu/Desktop");
        try {
            String folder = DateUtil.format(DateUtil.now(), "yyyy" + File.separatorChar + "MM" + File.separatorChar + "dd");
            File outFolder = new File(file.getAbsolutePath() + File.separator + folder);
            if (!outFolder.exists()) {
                outFolder.mkdirs();
            }
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 删除不需要的文件
     * 
     * @author monlyu
     * @since 2010-1-30
     */
    public void deleteFile(Long id){
        Attachment attachment = dao.get(Attachment.class, id);
        if (attachment == null) {
            LogUtil.log(getClass()).warn("删除无效的附件,可能文件丢失,附件ID" + id);
        } else {
            String filePath = WebOptUtil.APP_ARGS.getUploadDir() + attachment.file();
            FileUtils.deleteQuietly(new File(filePath));
            LogUtil.log(getClass()).info("删除的附件:" + filePath);
            FileUtils.deleteQuietly(new File(FileUtil.appendFileName(filePath, "_128")));
            LogUtil.log(getClass()).info("删除的附件:" + FileUtil.appendFileName(filePath, "_128"));
            dao.delete(Attachment.class, id);
        }
    }

    public static void main(String[] args){
        //File f = new File("\\Volumes\\Develop\\WorkStation\\WS4Java\\S4Me\\news4Desinger\\src\\core\\com\\monlyu\\bo\\AttachmentBo.java");
        String a = "\\common\\\\upload/2011/08/07\\8c3e5b4e-3783-4adf-ac90-09298513cf22_128.jpg";
        System.out.println(StringUtil.path2Uri(a));
        // new AttachmentBo().createDir();
    }
}
